package stz.flash.main.strobe.model;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import stz.flash.main.FlashScreen;
import util.DjDoneListener;


/**
 * 해상도 / 제거이후(on) / 제거이후(off) / AllDetectPixel(on) / AllDetectPixel(off)
 * 640x480/            0 /           117 /                 83 /                 172
 * 480x360/            0 /            91 /                 47 /                 120
 * 480x800/            5 /           137 /                 95 /					199 
 * 480x640/            0 /           130 /                    /
 * 360x480/        	   2 /            69 /                    /
 * 360x400/      	   2 /            69 /                    / 
 * 320x240/      	   3 /            35 /                    /
 */

public class InspectVRFlash {
	
	private static final int WIDTH_INSPECTION_AREA = 50;
	
	private static final int DEFUALT_HEIGHT_INSPECTION_AREA = 40;
	private static final int MAX_HEIGHT_INSPECTION_AREA = Display.getHeight() * 15 / 100;
	private static final int HEIGHT_INSPECTION_AREA = ( //검색영역이 위쪽까지 침범하는걸 방지
			(DEFUALT_HEIGHT_INSPECTION_AREA < MAX_HEIGHT_INSPECTION_AREA)) ? 
					DEFUALT_HEIGHT_INSPECTION_AREA : MAX_HEIGHT_INSPECTION_AREA;
	
	private static final int X_INSPECTION_AREA = Display.getWidth() * 70 / 100;
	private static final int Y_INSPECTION_AREA = 
			(Display.getHeight() - (HEIGHT_INSPECTION_AREA - FlashScreen.GAP_BOTTOM_px))
			- FlashScreen.GAP_BOTTOM_px;

	private static final int INSPECTION_SENSITIVITY = 150;

	private static final int NOISE_AFTER_DEL_OF_FLASH_ON = 15;

	private static final int MAXIMUM_ALL_DETECT_PIXEL = 300;
	private static final int MINIMUM_ALL_DETECT_PIXEL = 10;
	
	private static final int REPEAT_NUM_WAIT_VISIBLE_VR = 50;
	private static final int DELAY_WAIT_VISIBLE_VR = 30;
	
	static int[] argb = new int[WIDTH_INSPECTION_AREA * HEIGHT_INSPECTION_AREA];
	
	private static Bitmap inspectionBitmap = new Bitmap(WIDTH_INSPECTION_AREA, HEIGHT_INSPECTION_AREA);
	
	private static int countAllDetectPixel = 0;
	private static int countAlivePixel = 0;
	
	public static synchronized boolean isFlashState() {
		
		inspectPixcelOfScreentshot();
		
		//제거작업 이후값인 countAlivePixel이 0에 가까우면 플래시는 True로 추정
		//단 0은 VR이 아닌 화면에서도 충분히 나올수 있는 값이므로 신뢰도 떨어짐
		return (countAlivePixel < NOISE_AFTER_DEL_OF_FLASH_ON) ? true : false;
	}

	public static synchronized boolean surmiseVisibleVR(){

		inspectPixcelOfScreentshot();
		
		//countAllDetectPixel 가 0 또는 MAXIMUM_ALL_DETECT_PIXEL이상이라면
		//VR이 보이지 않는 상태일 확률이 높다
		return (countAllDetectPixel < MINIMUM_ALL_DETECT_PIXEL 
				|| countAllDetectPixel > MAXIMUM_ALL_DETECT_PIXEL ) ? false : true;
	}

	private static synchronized void inspectPixcelOfScreentshot() {
		
		Display.screenshot(
				inspectionBitmap, 
				X_INSPECTION_AREA, Y_INSPECTION_AREA , 
				WIDTH_INSPECTION_AREA, HEIGHT_INSPECTION_AREA);
		
		inspectionBitmap.getARGB(argb, 0, WIDTH_INSPECTION_AREA, 0, 0, 
				WIDTH_INSPECTION_AREA, HEIGHT_INSPECTION_AREA);
		
		countAlivePixel = 0;
		countAllDetectPixel = 0;
		
		for (int i = 0; i < HEIGHT_INSPECTION_AREA; i++){
			boolean initDetect = false;
			boolean delSwitch = false;
			
			for (int j = 0; j < WIDTH_INSPECTION_AREA; j++){
				
				int point = (i * WIDTH_INSPECTION_AREA) + j;
//				int a = argb[point] >> 24 & 0xFF;
				int r = argb[point] >> 16 & 0xFF;
				int g = argb[point] >> 8 & 0xFF;
				int b = argb[point] & 0xFF;
				
				if (r > INSPECTION_SENSITIVITY 
						&& g > INSPECTION_SENSITIVITY 
						&& b > INSPECTION_SENSITIVITY) {
			
					countAllDetectPixel++;
					
					if (initDetect == false) {
						initDetect = true;
						
						delSwitch = true;
					}
					
					if (delSwitch) {
//						argb[point] = 0;
					} else {
						countAlivePixel++;
//						argb[point] = -1;
					}
					
					
				} else {
//					argb[point] = 0;
					delSwitch = false;
				}
			}
		}
		
//		inspectionBitmap.setARGB(argb, 0, WIDTH_INSPECTION_AREA, 0, 0, 
//				WIDTH_INSPECTION_AREA, HEIGHT_INSPECTION_AREA);
	}
	
	public static void waitVisibleVR() {
		for (int i = 0; i < REPEAT_NUM_WAIT_VISIBLE_VR; i++) {
			try {
				Thread.sleep(DELAY_WAIT_VISIBLE_VR);
			} catch (InterruptedException e) {
			}
			if (InspectVRFlash.surmiseVisibleVR())
				break;
		}
	}
	
}